﻿#include "TurnTableCode_TurnTable_SetPositionMode.h"

#include "TurnTableCodec.h"

TurnTableCode_TurnTable_SetPositionMode::TurnTableCode_TurnTable_SetPositionMode(TurnTableCodec *turnTableCodec)
{
  this->turnTableCodec = turnTableCodec;
}

TurnTableCode_TurnTable_SetPositionMode::~TurnTableCode_TurnTable_SetPositionMode()
{
}

QByteArray TurnTableCode_TurnTable_SetPositionMode::code(float arg1, float arg2, float arg3)
{
  //arg1 position
  //arg2 rate
  //arg3 acceleration
  const float turnTablePositionLowerLimit = turnTableCodec->getTurnTablePositionLowerLimit();
  const float turnTablePositionUpperLimit = turnTableCodec->getTurnTablePositionUpperLimit();
  if (-360 < turnTablePositionLowerLimit) //如果转台位置下限大于-360，代表需要进行下限判断
    {
      if (turnTablePositionLowerLimit > arg1)//如果位置参数小于下限则返回
        {
          return QByteArray();
        }
    }
  if (360 > turnTablePositionUpperLimit) //如果转台位置下限小于360，代表需要进行上限判断
    {
      if (turnTablePositionUpperLimit < arg1)//如果位置参数大于上限则返回
        {
          return QByteArray();
        }
    }
  if(turnTableCodec->getTurnTableRateLowerLimit()>arg2)//如果速率参数小于下限则返回
    {
      return QByteArray();
    }
  if(turnTableCodec->getTurnTableRateUpperLimit()<arg2)//如果速率参数大于上限则返回
    {
      return QByteArray();
    }
  if(turnTableCodec->getTurnTableAccelerationLowerLimit()>arg3)//如果加速度参数小于下限则返回
    {
      return QByteArray();
    }
  if(turnTableCodec->getTurnTableAccelerationUpperLimit()<arg3)//如果加速度参数大于上限则返回
    {
      return QByteArray();
    }
  //如果参数没有超限，则返回组装的指令
  return addSummnation(QByteArray().append((uchar)TurnTableCodec::TurnTable_DeviceNum)
                       .append(1,(uchar)TurnTableCodec::TurnTable_AxleNumOne)
                       .append((uchar)TurnTableCodec::TurnTable_SetPositionMode)
                       .append(floatToByteArray(arg1))
                       .append(floatToByteArray(arg2))
                       .append(floatToByteArray(arg3)));
}
